<?php
$bonus_type = array(
    '1'=>'推荐微店',
    '2'=>'销售返利',
    '3'=>'服务奖',
    '4'=>'推广任务',
    '5'=>'销售冲单',
);

$str = "";
if($_GET['member_id']){
    $memIds = str_replace('，', ',', $_GET['member_id']);
    $str .= " and a.member_id in({$memIds})";
}
if($_GET['type']){
    $str .= " and a.type={$_GET['type']}";
}
if(isset($_GET['status']) && $_GET['status']!=''){
    $str .= " and a.status='".$_GET['status']."'";
}
if($_GET['stime']){
    $str .= " and a.create_time>'{$_GET['stime']}'";
}
if($_GET['etime']){
    $str .= " and a.create_time<'{$_GET['etime']}'";
}
if($_GET['record_id']>0){
    $str .= " and a.record_id='{$_GET['record_id']}'";
}

$sql = "select a.*,b.name as shop_name from ".MICROSHOPBONUSDETAIL." AS a left join ".MICROSHOP." AS b on a.member_id=b.member_id where 1 {$str} order by a.create_time desc";

if(!($_GET['relation_id']>0)){
    //=======分页=============
    include_once("../includes/page_utf_class.php");
    $page = new Page;
    $page->listRows=20;
    $totalRows = $db->num_rows();
    if (!$page->__get('totalRows')){
        $db->query($sql);
        $de['count'] = $page->totalRows = $db->num_rows();
    }
    $sql .= "  limit ".$page->firstRow.",".$page->listRows;
    $de['count']=$de['count']?$de['count']:($_GET['totalRows']?$_GET['totalRows']:'0');
    $de['page'] = $page->prompt();
    $de['perPageRows'] = $page->listRows;
    //=====================
}
$db->query($sql);
$de['list']=$db->getRows();
$de['total_price'] = 0;//总金额
//总金额
$de['total_price'] = 0;
$sql = "select sum(a.price) as total_price from ".MICROSHOPBONUSDETAIL." AS a left join ".MICROSHOP." AS b on a.member_id=b.member_id where 1 {$str}";
$db->query($sql);
$de['total_price'] = $db->fetchField('total_price');
foreach ($de['list'] as $k=>$v)
{
    $de['list'][$k]['type'] = $bonus_type[$v['type']];
    //订单ID或推荐店铺ID
    $relation_id = '';
    switch ($v['type'])
    {
        case 1:
            $sql = "select rec_member_id from ".MICROSHOPBONUSEXPAND." where bonus_id={$v['id']}";
            $db->query($sql);
            $relation_id = $db->fetchField('rec_member_id');
            break;
        case 2:
            $sql = "select order_id from ".MICROSHOPBONUSSALES." where bonus_id={$v['id']}";
            $db->query($sql);
            $relation_id = $db->fetchField('order_id');
            break;
        case 3:
            $sql = "select order_id from ".MICROSHOPBONUSSERVICE." where bonus_id={$v['id']}";
            $db->query($sql);
            $relation_id = $db->fetchField('order_id');
            break;
        default:
            break;
    }
    
    $de['list'][$k]['relation_id'] = $relation_id;
}
if($_GET['relation_id']>0){
    $list = $de['list'];
    $de['list'] = [];
    foreach ($list as $val)
    {
        if($val['relation_id'] == $_GET['relation_id']){
            $de['list'][] = $val;
        }
    }
}

$tpl->assign('de',$de);
$tpl->assign('config',$config);
$tpl->assign('bonus_type',$bonus_type);
$tpl->display("shop_bonus_list.htm");